package com.leostadyn.stackfarm.timestatistics.base;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.content.LocalBroadcastManager;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;

import com.leostadyn.stackfarm.timestatistics.common.Constants;
import com.leostadyn.stackfarm.timestatistics.utils.ToastUtil;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import io.reactivex.annotations.NonNull;
import io.reactivex.disposables.CompositeDisposable;
import io.reactivex.disposables.Disposable;

public abstract class BaseActivity extends AppCompatActivity {
    protected CompositeDisposable compositeDisposable = new CompositeDisposable();
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    private BroadcastReceiver broadcastReceiver;

    @Override
    public boolean moveTaskToBack(boolean nonRoot) {
        return super.moveTaskToBack(true);
    }


    public void showToast(String msg) {
        ToastUtil.show(this, msg);
    }

    protected void addDisposable(@NonNull Disposable d) {
        if (compositeDisposable != null) {
            compositeDisposable.add(d);
        }
    }

    protected void removeDisposable(@NonNull Disposable d) {
        if (compositeDisposable != null) {
            compositeDisposable.delete(d);
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        registerBroadcastReceiver();
    }

    @Override
    protected void onPause() {
        super.onPause();
        try {
            View view = getCurrentFocus();
            if (view != null) {
                InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                inputMethodManager.hideSoftInputFromWindow(view.getWindowToken(), 0);
            }
        } catch (Exception ex) {
        }

        if (null != broadcastReceiver) {
            LocalBroadcastManager.getInstance(BaseActivity.this).unregisterReceiver(broadcastReceiver);
        }
    }

    private void registerBroadcastReceiver() {
        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction(Constants.SHOWMSG);
        broadcastReceiver = new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {
                showToast(intent.getStringExtra(Constants.MSGINFO));
            }
        };
        LocalBroadcastManager.getInstance(BaseActivity.this).registerReceiver(broadcastReceiver, intentFilter);
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        // 当前的Activity销毁，需要销毁所有的网络请求。
        if (compositeDisposable != null) {
            compositeDisposable.dispose();
            compositeDisposable.clear();
        }
    }

    /**
     * check input is null or not
     */
    public boolean isEmpty(EditText et) {
        return TextUtils.isEmpty(et.getText());
    }

    protected boolean isEmpty(String info) {
        return TextUtils.isEmpty(info);
    }

    protected boolean notEmpty(String info) {
        return !isEmpty(info);
    }

    protected boolean isEmpty(Map map) {
        return map == null || map.isEmpty();
    }

    protected boolean isEmpty(List list) {
        return list == null || list.isEmpty();
    }

    protected <T> boolean isNull(T t) {
        return t == null;
    }

    protected <T> boolean notNull(T t) {
        return t != null;
    }


    public HashMap<String, String> getBaseParams() {
        HashMap<String, String> params = new HashMap<>();
//        params.put("lang", LocaleHelper.getLanguage(this));
        return params;
    }

    public HashMap<String, String> getBaseParamsNoTonken() {
        HashMap<String, String> params = new HashMap<>();
//        params.put("lang", LocaleHelper.getLanguage(this));
        return params;
    }


    public String getEditString(EditText editText) {
        return editText.getText().toString().trim();
    }

    public boolean haveString(EditText editText) {
        String s = editText.getText().toString().trim();
        return !TextUtils.isEmpty(s);
    }

    public void hideInput() {
        try {
            View view = getCurrentFocus();
            if (view != null) {
                InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                inputMethodManager.hideSoftInputFromWindow(view.getWindowToken(), 0);
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }

    }

}
